<html>
<head><meta charset="utf-8"><title>How do I get the module from a `DefId` · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html">How do I get the module from a `DefId`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216590780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216590780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216590780">(Nov 13 2020 at 10:06)</a>:</h4>
<p>I would like to get the module (probably some kind of id), then being able to generate its string representation.<br>
I found <code>tcx.def_path(function)</code> which give me  a <code>DefPath</code>, but I'm not sure if it's the right direction.</p>
<p>For example, if <code>function</code> is a <code>DefId</code>, and <code>tcx.def_path_str(function) == "m1::m2::foo"</code>, then I would like to have a function <code>tcx.get_module</code> such as <code>get_module(function)</code> gives me some id (probably a <code>DefID</code>), then <code>tcx.def_path_str(tcx.get_module(function)) == "m1::m2"</code>.</p>



<a name="216592218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216592218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216592218">(Nov 13 2020 at 10:24)</a>:</h4>
<p>This kind of work, but 1) this feels very, very hacky and 2) I would like to not have the extra <code>::</code> at the beginning:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">module</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">def_path</span><span class="p">(</span><span class="n">function</span><span class="p">);</span><span class="w"></span>
<span class="n">module</span><span class="p">.</span><span class="n">data</span><span class="p">.</span><span class="n">pop</span><span class="p">();</span><span class="w"> </span><span class="c1">// remove the function name for the DefPath</span>
<span class="n">eprintln</span><span class="o">!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">module</span><span class="p">.</span><span class="n">to_string_no_crate_verbose</span><span class="p">());</span><span class="w"></span>
</code></pre></div>
<p>For the function <code>m1::m2::foo</code> I get <code>::m1::m2</code> instead of <code>m1::m2</code>.</p>



<a name="216598281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216598281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216598281">(Nov 13 2020 at 11:31)</a>:</h4>
<p>It's not even right. For things like <code>std::ops::Fn::call</code> I should get <code>std::ops</code>, but my hacky method gives <code>::std::ops::Fn</code> (that Include the name of the trait)</p>



<a name="216609263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216609263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216609263">(Nov 13 2020 at 13:25)</a>:</h4>
<p><span class="user-mention" data-user-id="281841">@Robin Moussu</span> there's some code for this in collect_intra_doc_links, feel free to turn it into a function on tcx</p>



<a name="216609283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216609283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216609283">(Nov 13 2020 at 13:26)</a>:</h4>
<p>Ideally it would be a query</p>



<a name="216609348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216609348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216609348">(Nov 13 2020 at 13:26)</a>:</h4>
<p>It's the bit that says "the parent may not always be a module"</p>



<a name="216627492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216627492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216627492">(Nov 13 2020 at 15:36)</a>:</h4>
<p>While we are here, what should a use to get the url of the doc of a function? For example, I would like to get <code>target/doc/test1/m2/fn.bar.html</code> from the <code>DefId</code> of  the function <code>m2::bar</code> in the crate <code>test1</code>.</p>



<a name="216627597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216627597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216627597">(Nov 13 2020 at 15:36)</a>:</h4>
<p><span class="user-mention" data-user-id="281841">@Robin Moussu</span> I answered on discord, but <code>fn href</code></p>



<a name="216627611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216627611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216627611">(Nov 13 2020 at 15:37)</a>:</h4>
<p>it's somewhere in <code>clean</code> I think</p>



<a name="216642777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216642777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216642777">(Nov 13 2020 at 17:17)</a>:</h4>
<p>ah, that's where I asked it. I couldn't remember!!!</p>



<a name="216645729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216645729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216645729">(Nov 13 2020 at 17:37)</a>:</h4>
<p>Why is <code>crate::html::format::href(my_function_id)</code> returning <code>None</code>?</p>



<a name="216645839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216645839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216645839">(Nov 13 2020 at 17:38)</a>:</h4>
<p>I tried it with <code>--document-private-items</code>, and some functions are public</p>



<a name="216645909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216645909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216645909">(Nov 13 2020 at 17:38)</a>:</h4>
<p>You need the documentation from other crates to be available</p>



<a name="216645939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216645939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216645939">(Nov 13 2020 at 17:39)</a>:</h4>
<p>The easy workaround for now is to use --extern-html-root-url</p>



<a name="216645992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216645992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216645992">(Nov 13 2020 at 17:39)</a>:</h4>
<p>If you ran this as part of <code>cargo doc</code> it should just work <span aria-label="fingers crossed" class="emoji emoji-1f91e" role="img" title="fingers crossed">:fingers_crossed:</span></p>



<a name="216646281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646281">(Nov 13 2020 at 17:41)</a>:</h4>
<p>I am, but it doesn't.</p>
<div class="codehilite" data-code-language="Bash"><pre><span></span><code>$ cargo run -- -Z unstable-options --extern-html-root-url test/test1/src/main.rs
    Finished dev <span class="o">[</span>unoptimized + debuginfo<span class="o">]</span> target<span class="o">(</span>s<span class="o">)</span> in <span class="m">0</span>.05s
     Running <span class="sb">`</span>target/debug/cargo-callgraph -Z unstable-options --extern-html-root-url test/test1/src/main.rs<span class="sb">`</span>
error: missing file operand
</code></pre></div>



<a name="216646360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646360">(Nov 13 2020 at 17:42)</a>:</h4>
<p><a href="https://github.com/Nemo157/dotfiles/blob/master/bin/cargo-doc-like-docs.rs#L72-L73">https://github.com/Nemo157/dotfiles/blob/master/bin/cargo-doc-like-docs.rs#L72-L73</a></p>



<a name="216646401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646401">(Nov 13 2020 at 17:42)</a>:</h4>
<p><span class="user-mention" data-user-id="281841">@Robin Moussu</span> that's not part of cargo doc</p>



<a name="216646421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646421">(Nov 13 2020 at 17:43)</a>:</h4>
<p>I mean if you inserted this as a shim and replaced the rustdoc binary</p>



<a name="216646681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646681">(Nov 13 2020 at 17:45)</a>:</h4>
<p>What I'm doing is that I took the rustdoc source, then modified the content of <code>core.rs</code>, more specifically <code>run_global_ctxt</code>. So I'm expecting that the arguments would be parsed correctly. But maybe I didn't understand what you meant.</p>



<a name="216646828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646828">(Nov 13 2020 at 17:46)</a>:</h4>
<p>I don't have time right now, ask in #rustdoc</p>



<a name="216646857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646857">(Nov 13 2020 at 17:46)</a>:</h4>
<p>you're runing rustdoc standalone instead of it getting called by cargo</p>



<a name="216646915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20module%20from%20a%20%60DefId%60/near/216646915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20module.20from.20a.20.60DefId.60.html#216646915">(Nov 13 2020 at 17:47)</a>:</h4>
<p>ok</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>